package com.example.myblog1_0.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.myblog1_0.model.MyUser;
import com.example.myblog1_0.service.ArticleService;
import com.example.myblog1_0.service.Impl.QqAuthServiceImpl;
import com.example.myblog1_0.service.UserService;
import com.example.myblog1_0.util.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.rpc.ServiceException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Map;

/**
 * @Author: xc
 * @Date: 2018/12/13 18:07
 * @Description:
 **/
@Controller
public class MyController {
    @Autowired
    QqAuthServiceImpl qqAuthService;
    @Autowired
    UserService userService;


    @GetMapping("/")
    public String index(HttpServletResponse response, HttpServletRequest request,
                        @AuthenticationPrincipal Principal principal){
        String username ;
        try {
            username = principal.getName();

        }catch (NullPointerException e){
            request.getSession().removeAttribute("lastUrl");
            return "index";
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("lastUrl", (String) request.getSession().getAttribute("lastUrl"));
        response.setHeader("username",username);

        return "index";
    }
    @GetMapping("/index")
    public String index2(){
        return "index";
    }
    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @GetMapping("/qqLogin")
    public String qqLogin() throws UnsupportedEncodingException {
        String url = qqAuthService.getAuthorizationUrl();
        return "redirect:" + url;
    }
    @GetMapping("/qq")
    public String qq(String code ,HttpServletRequest request) throws ServiceException {
        String accessToken = qqAuthService.getAccessToken(code);
        String openid = qqAuthService.getOpenId(accessToken);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        MyUser findUser = userService.findUserInfoByOpenid(openid);
        if (findUser == null){
            MyUser buildUser = new MyUser();
            JSONObject object = qqAuthService.getUserInfo(accessToken,openid);

            String nickname = object.getString("nickname");
            buildUser.setNickname(nickname);

            String username = code;
            buildUser.setUsername(username);


            String password = bCryptPasswordEncoder.encode(username);
            buildUser.setPassword(password);

            buildUser.setRole("ROLE_USER");

            buildUser.setOpenid(openid);

            userService.insetUser(buildUser);
            request.getSession().removeAttribute("lastUrl");
            return "/login";
        }else {
            String username = code;
            findUser.setUsername(username);

            String password = bCryptPasswordEncoder.encode(username);
            findUser.setPassword(password);

            userService.updateQqUser(findUser);
            request.getSession().removeAttribute("lastUrl");
            return "/login";
        }
    }

    @GetMapping("/editor")
    public String editor(HttpServletRequest request){
        request.getSession().removeAttribute("lastUrl");
        return "editor";
    }
    @GetMapping("/superAdmin")
    public String superAdmin(HttpServletRequest request){
        request.getSession().removeAttribute("lastUrl");
        return "superadmin";
    }
    @GetMapping("/article")
    public String categories(HttpServletRequest request){
        request.getSession().removeAttribute("lastUrl");
        return "article";
    }
    @GetMapping("/leaveMessage")
    public String leaveMessage(HttpServletResponse response, HttpServletRequest request,
                               @AuthenticationPrincipal Principal principal){
        String username ;
        try {
             username = principal.getName();


        }catch (NullPointerException e){
            request.getSession().removeAttribute("lastUrl");
            return "index";
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("lastUrl", (String) request.getSession().getAttribute("lastUrl"));
        response.setHeader("username",username);
        return "leaveMessage";
    }
    @GetMapping("/about")
    public String aboutMe(){
        return "about";
    }
    @GetMapping("/user")
    public String user(){
        return "user";
    }
    @GetMapping("/isLogin")
    public @ResponseBody JSONObject isLogin(@AuthenticationPrincipal Principal principal){
        JSONObject jsonObject = new JSONObject();
        try {
            String username = principal.getName();
            if (username == null){
                jsonObject.put("status",0);
                jsonObject.put("message","暂无用户登陆");
                return  jsonObject;
            }else {
                jsonObject.put("status",1);
                return  jsonObject;
            }
        }catch (Exception e){
            jsonObject.put("status",0);
            jsonObject.put("message","暂无用户登陆");
            return  jsonObject;
        }
    }
}


